package com.yystudy.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yystudy.common.result.Result;
import com.yystudy.model.system.SysRole;
import com.yystudy.model.vo.AssginRoleVo;
import com.yystudy.model.vo.SysRoleQueryVo;
import com.yystudy.system.annotation.Log;
import com.yystudy.system.enums.BusinessType;
import com.yystudy.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysRole")
@Api(tags = "角色管理接口")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 给用户分配角色
     * @param assginRoleVo
     * @return
     */
    @ApiOperation(value = "给用户分配角色")
    @PostMapping("/doAssign")
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @Log(title = "角色管理",businessType = BusinessType.ASSGIN)
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }


    /**
     * 根据用户id获取角色数据
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户id获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.getRoleByUserId(userId);
        return Result.ok(roleMap);
    }

    /**
     * 查询所有角色数据（弃用）
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result<List<SysRole>> findAll() {
        List<SysRole> list = sysRoleService.list();
        Result<List<SysRole>> ok = Result.ok(list);
        return ok;
    }


    /**
     * 条件分页查询
     *
     * @param page
     * @param limit
     * @param sysRoleQueryVo
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findPageQueryRole(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    SysRoleQueryVo sysRoleQueryVo) {
        //创建page对象
        Page<SysRole> sysRolePage = new Page<>(page, limit);
        IPage<SysRole> pageModel = sysRoleService.selectPage(sysRolePage, sysRoleQueryVo);
        return Result.ok(pageModel);
    }


    /**
     * 添加角色
     *
     * @param sysRole
     * @return
     */
    @Log(title = "角色管理",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole) {
        boolean save = sysRoleService.save(sysRole);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "根据id查询数据")
    @GetMapping("/findSysRoleById/{id}")
    public Result<SysRole> findById(@PathVariable Long id) {
        SysRole byId = sysRoleService.getById(id);
        return Result.ok(byId);
    }


    /**
     * 更新数据
     *
     * @param sysRole
     * @return
     */
    @Log(title = "角色管理",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "更新数据")
    @PostMapping("/update")
    public Result update(@RequestBody SysRole sysRole) {
        boolean b = sysRoleService.updateById(sysRole);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    @Log(title = "角色管理",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        sysRoleService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除，多个id值：[2,3,5,7]
     *
     * @param idList
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据id列表批量删除")
    @DeleteMapping("/batchRemove")
    @Log(title = "角色管理",businessType = BusinessType.DELETE)
    public Result batchRemove(@RequestBody List<Long> idList) {
        sysRoleService.removeByIds(idList);
        return Result.ok();
    }


}
